package com.hoow.shakoow.extend

import android.annotation.SuppressLint
import androidx.core.text.isDigitsOnly
import com.google.gson.Gson
import java.io.File
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

fun String?.toStringArray(): List<String> {
    try {
        return Gson().fromJson(this, mutableListOf<String>().javaClass)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return mutableListOf()
}

fun String?.isYes(): Boolean {
    return this.orEmpty().equals("YES", true)
}

fun String?.isNo(): Boolean {
    return this.orEmpty().equals("NO", true)
}

fun String.prefixWithCurrencyUnit(): String {
    if (this.isBlank()) {
        return ""
    }
    return this
}

fun String.safeString(): String {
    if (this.isBlank()) {
        return ""
    }
    return this
}

fun String.safeInt(): Int {
    if (this.isBlank()) {
        return 0
    }
    if (this.isDigitsOnly()) {
        return this.toInt()
    }
    return 0
}

fun String.prefixWithAddUnit(): String {
    return ("+$this")
}


fun String?.multiplyValue(other: String?): String? {
    try {
        this?.let { selfResult ->
            other?.let { otherResult ->
                val selfDecimal = BigDecimal(selfResult)
                val otherDecimal = BigDecimal(otherResult)
                val resultDecimal = selfDecimal.multiply(otherDecimal)
                return resultDecimal.stripTrailingZeros().toPlainString()
            }
            return selfResult.toFinanceString()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return this.orEmpty().toFinanceString()
}

fun String.toFinanceString(): String {
    if (this.isNotBlank()) {
        val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        return resultDecimal.toPlainString()
    }
    return this
}

fun String.toFinancePercentString(): String {
    if (this.isNotBlank()) {
        val valueDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        val edgeDecimal = BigDecimal(100)
        val resultDecimal: BigDecimal = valueDecimal.multiply(edgeDecimal)
        var result = resultDecimal.stripTrailingZeros().toPlainString()
        if (result.equals("0.0") or
            result.equals("0.00") or
            result.equals("0.000") or
            result.equals("0.0000")
        ) {
            result = "0"
        }
        return "${result}%"
    }
    return this
}

fun String.toFinanceStringWithMoneyFlag(): String {
    if (this.isNotBlank()) {
        val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        return resultDecimal.toPlainString()
    }
    return this
}

fun String.toEncryptPhoneNumber(): String {
    if (this.isNotBlank()) {
        val firstLine = if (this.length >= 3) this.substring(0, 3) else this
        val lastLine = if (this.length >= 6) this.substring(this.length - 3) else ""
        return "$firstLine **** $lastLine"
    }
    return this
}

fun String.toPhoneNumberInputHint(): String {
    if (this.isNotBlank()) {
        if (this.length >= 4) {
            var tempString = ""
            if (tempString.isBlank()) {
                tempString = this.substring(0, 4)
            }

            if (this.length >= 7) {
                tempString += " "
                tempString += this.substring(4, 7)
            }

            if (this.length >= 10) {
                tempString += " "
                tempString += this.substring(7)
            }
            return tempString

        } else {
            return this
        }
    }
    return this
}

fun String.filterForPhoneNumber(): String {
    val regEx = "[^0-9]"
    val p = Pattern.compile(regEx)
    val m = p.matcher(this)
    return m.replaceAll("").trim()
}

fun String.isValidLink(): Boolean {
    return (this.safeString().startsWith("http"))
}

fun String.isEmailFormat(): Boolean {
    if (this.safeString().isNotBlank()) {
        return this.contains("@")
    }
    return false
}

fun String.suffixPath(): String {
    try {
        val file = File(this)
        return file.name
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return this
}

@SuppressLint("SimpleDateFormat")
fun String.dateForFormatString(targetFormat: String): Date? {
    try {
        val sDateFormat = SimpleDateFormat(targetFormat)
        val sdate = sDateFormat.parse(this)
        return sdate
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

fun String.toWords(): String {
    if (this.isBlank()) {
        return ""
    }

    var tempString = ""
    val wordList = this.split("_")
    try {
        for (index in 0 until wordList.size) {
            val word = wordList[index]
            if (word.isNotBlank()) {
                val capi = word.substring(0, 1).uppercase()
                val last = word.substring(1).lowercase()
                if (tempString.isBlank()) {
                    tempString = (capi + last)
                } else {
                    tempString = tempString + " " + (capi + last)
                }
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return tempString
}
